package org.go.scheduler.rmi;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.concurrent.ConcurrentHashMap;

import org.go.expcetion.GoException;
import org.go.expcetion.JobPersistenceException;
import org.go.expcetion.SchedulerException;
import org.go.log.Log;
import org.go.log.LoggerFactory;
import org.go.scheduler.Scheduler;
import org.go.scheduler.SchedulerResources;
import org.go.scheduler.memory.MemoryScheduler;
import org.go.trigger.Trigger;
import org.go.trigger.TriggerKey;
import org.go.work.Work;

/**
 * 
 * @author hejie
 *
 */
public class RMIServerScheduler extends UnicastRemoteObject implements IRmiScheduler {
	/**/
	private static final Log log = LoggerFactory.getLog(RMIServerScheduler.class);

	Scheduler scheduler;

	/**
	 * 
	 */
	private static final long serialVersionUID = 1389240245299521808L;

	protected RMIServerScheduler() throws RemoteException {
		super();
	}

	protected RMIServerScheduler(SchedulerResources resources) throws RemoteException, SchedulerException {
		super();
		try {
			setScheduler(new MemoryScheduler(resources));
		} catch (Exception e) {
			throw new SchedulerException(e);
		}
		//scheduler.getResource().setWorkerStore(new WorkStoreMemory());
	}

	@Override
	public ConcurrentHashMap<String, Object> getContext() throws SchedulerException, RemoteException {
		return null;
	}

	@Override
	public String getJobGroupNames() throws SchedulerException, RemoteException {
		return null;
	}

	public Scheduler getScheduler() {
		return scheduler;
	}

	@Override
	public String getSchedulerName() throws SchedulerException, RemoteException {
		return scheduler.getSchedulerName();
	}

	@Override
	public Trigger getTrigger(TriggerKey tKey) throws SchedulerException, RemoteException {
		return null;
	}

	@Override
	public boolean isShutdown() throws SchedulerException, RemoteException {
		return scheduler.isShutdown();
	}

	@Override
	public void notifySchedulerListenersError(String string, GoException se) throws SchedulerException, RemoteException {

	}

	@Override
	public void notifySchedulerListenersError(String string, JobPersistenceException se) throws SchedulerException, RemoteException {

	}

	/*
	 * (non-Javadoc)
	 * @see org.go.core.IRmiScheduler#addWorker(org.go.WorkerDesc)
	 */
	@Override
	public void scheduler(Work workerDesc, Trigger trigger) throws SchedulerException, RemoteException {
		scheduler.scheduler(workerDesc, trigger);
	}

	public void setScheduler(Scheduler scheduler) {
		this.scheduler = scheduler;
	}

	@Override
	public void shutdown(boolean cleanShutdown) throws SchedulerException, RemoteException {

	}

	@Override
	public void shutdownNow() throws SchedulerException, RemoteException {

	}

	@Override
	public void start() throws SchedulerException, RemoteException {

	}

	//@Override
	//	public void startAll() throws SchedulerException, RemoteException {
	//		//		log.debug("startAll");
	//		//		try {
	//		//			//scheduler.startAll();
	//		//		} catch (GoException e) {
	//		//			log.error("{}", e.getMessage());
	//		//		}
	//	}

	@Override
	public void startDelayed(int seconds) throws SchedulerException, RemoteException {

	}

	@Override
	public void startWorker(String workerId) throws SchedulerException, RemoteException {

	}

	@Override
	public void unscheduleWork(Work workerDesc) throws SchedulerException, RemoteException {

	}

	@Override
	public void startAll() throws SchedulerException, RemoteException {

	}

	@Override
	public void pauseAllWorks() throws SchedulerException, RemoteException {
		scheduler.pauseAllWorks();
	}
}
